package com.example.active.sf;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/17 8:23
 */
public class Producer
{
    public static final String ACTIVEMQ_URL = "nio://192.168.0.118:61618";
//    public static final String ACTIVEMQ_URL = "failover:(tcp://192.168.0.118:61616," +
//        "tcp://192.168.0.118:61617," +
//        "tcp://192.168.0.118:61618)?randomize=false";
    static ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

    public static void main(String[] args) throws JMSException
    {
//        开启异步发送
//        factory.setUseAsyncSend(true);

        Connection connection = factory.createConnection();
        connection.start();
        //第一个是事务，第二个是签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        try
        {

            //创建目的地
            Queue queue = session.createQueue("queue01");
            //创建消息生产者
//            ActiveMQMessageProducer producer = (ActiveMQMessageProducer)session.createProducer(queue);
            MessageProducer producer = session.createProducer(queue);
//            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            TextMessage textMessage;
            for (int i = 0; i < 3; i++)
            {
                //创建消息
                textMessage = session.createTextMessage("msg-->" + i);
//                textMessage.setJMSMessageID("ID--> "+ i);
//                String messageID = textMessage.getJMSMessageID();

                /*延迟与重试配置
                textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY,3000);
                textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD,4000);
                textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT,5);*/
//                textMessage.setStringProperty("01","vip");
//                MapMessage mapMessage = session.createMapMessage();
//                mapMessage.setInt("v1", i);
                producer.send(textMessage);
//                producer.send(textMessage, new AsyncCallback()
//                {
//                    @Override
//                    public void onSuccess()
//                    {
//                        System.out.println("成功的消息："+messageID);
//                    }
//
//                    @Override
//                    public void onException(JMSException e)
//                    {
//                        System.out.println("失败的消息："+messageID);
//                    }
//                });
            }
//            session.commit();
            producer.close();
//            session.close();
            connection.close();
            System.out.println("消息发布到MQ");
        } catch (JMSException e)
        {
            e.printStackTrace();
//            session.rollback();
        } finally
        {
            if (null != session)
            {
                session.close();
            }
        }
    }
}
